﻿''' <summary>
''' represents player and player state (moves, wins)
''' </summary>
''' <remarks></remarks>
Public Class Player

  Public Enum TurnStateE
    MayMakeFirstMove
    MayMakeSecondMove
    WinsTurn
    LoosesTurn
    Inactive
  End Enum

  Private state As TurnStateE = TurnStateE.Inactive
  ''' <summary>
  ''' state of the turn the player has.
  ''' </summary>
  Public ReadOnly Property TurnState() As TurnStateE
    Get
      Return state
    End Get
  End Property

  ''' <summary>
  ''' publishes changes in the turnstate
  ''' </summary>
  Public Event TurnStateChanged(ByVal sender As Object, ByVal e As EventArgs)

  ''' <summary>
  ''' change the turn state. Event TurnStateChanged is raised. 
  ''' </summary>
  Friend Sub SetTurnState(ByVal state As TurnStateE)
    Me.state = state
    RaiseEvent TurnStateChanged(Me, New EventArgs)
  End Sub

  Private playerName As String
  ''' <summary>
  ''' name of this player
  ''' </summary>
  Public ReadOnly Property Name() As String
    Get
      Return Me.playerName
    End Get
  End Property

  ''' <summary>
  ''' constructor
  ''' </summary>
  ''' <param name="name">name of the player</param>
  Public Sub New(ByVal name As String)
    Me.playerName = name
  End Sub

  Private id As Integer
  ''' <summary>
  ''' ID with which Game can identify player easily
  ''' </summary>
  Friend Property InternalId() As Integer
    Get
      Return Me.id
    End Get
    Set(ByVal value As Integer)
      Me.id = value
    End Set
  End Property

  ''' <summary>
  ''' publishes changes in the won cards
  ''' </summary>
  Public Event ScoreChanged(ByVal sender As Object, ByVal e As EventArgs)

  Private cardsWon As New List(Of Card)
  ''' <summary>
  ''' add a set of cards after a win. Fires event CardsWonChanged.
  ''' </summary>
  ''' <param name="card1">the first card</param>
  ''' <param name="card2">the second card</param>
  ''' <remarks>the cards should (of course) be a twin pair, otherwise you will be severely punished by 
  ''' an argumentexception</remarks>
  Friend Sub AddCardsetAfterWin(ByVal card1 As Card, ByVal card2 As Card)
    If card1.TwinCard IsNot card2 OrElse card2.TwinCard IsNot card1 Then
      Throw New ArgumentException("Cardset should be a set of twins")
    End If

    cardsWon.Add(card1)
    cardsWon.Add(card2)

    RaiseEvent ScoreChanged(Me, New EventArgs)
  End Sub

  ''' <summary>
  ''' all won cards of this player
  ''' </summary>
  Public ReadOnly Property Score() As Integer
    Get
      Return cardsWon.Count
    End Get
  End Property
End Class
